﻿namespace SageKing.Database.SqlSugar.Service;

/// <summary>
/// 实体操作基服务
/// </summary>
/// <typeparam name="TEntity"></typeparam>
public class BaseService<TEntity> : IBaseService<TEntity, long>
    where TEntity : EntityBase, new()
{
    private readonly SageKingRepository<TEntity> _rep;

    public BaseService(SageKingRepository<TEntity> rep)
    {
        _rep = rep;
    }

    /// <summary>
    /// 获取详情
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [DisplayName("获取详情")]
    public virtual async Task<TEntity> GetDetail(long id)
    {
        return await _rep.GetByIdAsync(id);
    }

    /// <summary>
    /// 获取集合
    /// </summary>
    /// <returns></returns>
    [DisplayName("获取集合")]
    public virtual async Task<List<TEntity>> GetList()
    {
        return await _rep.GetListAsync();
    }

    /// <summary>
    /// 获取集合
    /// </summary>
    /// <returns></returns>
    [DisplayName("获取集合")]
    public virtual async Task<List<TEntity>> GetList(Expression<Func<TEntity, bool>> wherefunc)
    {
        return await _rep.AsQueryable().Where(wherefunc).ToListAsync();
    }

    /// <summary>
    /// 获取集合
    /// </summary>
    /// <returns></returns>
    [DisplayName("获取集合")]
    public virtual async Task<List<TEntity>> GetList(Expression<Func<TEntity, bool>> wherefunc, Expression<Func<TEntity, object>> orderByColumns, bool orderByType)
    {
        return await _rep.AsQueryable().Where(wherefunc).OrderBy(orderByColumns, orderByType ? OrderByType.Asc : OrderByType.Desc).ToListAsync();
    }

    /// <summary>
    /// 获取实体分页
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [DisplayName("获取实体分页")]
    public virtual async Task<PageBase<TEntity>> GetPageAsync(PageBaseInput input)
    {
        return await _rep.AsQueryable().ToPagedListAsync(input.Page, input.PageSize);
    }

    /// <summary>
    /// 获取实体分页
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [DisplayName("获取实体分页")]
    public virtual async Task<PageBase<TEntity>> GetPage(PageBaseInput input, Expression<Func<TEntity, object>> orderByColumns, bool orderByType)
    {
        return await _rep.AsQueryable().OrderBy(orderByColumns, orderByType ? OrderByType.Asc : OrderByType.Desc).ToPagedListAsync(input.Page, input.PageSize);
    }

    /// <summary>
    /// 获取实体分页
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [DisplayName("获取实体分页")]
    public virtual async Task<PageBase<TEntity>> GetPage(PageBaseInput input, List<(bool, Expression<Func<TEntity, bool>>)> whereIf, Expression<Func<TEntity, object>> orderByColumns, bool orderByType = true)
    {
        var querable = _rep.AsQueryable();
        foreach (var item in whereIf)
        {
            querable = querable.WhereIF(item.Item1, item.Item2);
        }
        return await querable.OrderBy(orderByColumns, orderByType ? OrderByType.Asc : OrderByType.Desc).ToPagedListAsync(input.Page, input.PageSize);
    }

    /// <summary>
    /// 增加
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    [DisplayName("增加")]
    public virtual async Task<bool> Add(TEntity entity)
    {
        return await _rep.InsertAsync(entity);
    }


    /// <summary>
    /// 增加
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    [DisplayName("增加")]
    public async Task<bool> Add(IEnumerable<TEntity> entity)
    {
        return await _rep.InsertRangeAsync(entity.ToArray());
    }
    /// <summary>
    /// 增加 返回id
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    [DisplayName("增加")]
    public virtual async Task<long> AddReturnId(TEntity entity, int type = 0)
    {
        switch (type)
        {
            case 0:
                return await _rep.InsertReturnSnowflakeIdAsync(entity);
            case 1:
                return await _rep.InsertReturnBigIdentityAsync(entity);
            case 2:
                return await _rep.InsertReturnIdentityAsync(entity);
            default:
                return await _rep.InsertReturnSnowflakeIdAsync(entity);
        }
    }

    /// <summary>
    /// 增加
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    [DisplayName("增加")]
    public virtual async Task<TEntity> AddReturnEntityAsync(TEntity entity)
    {
        return await _rep.InsertReturnEntityAsync(entity);
    }

    /// <summary>
    /// 增加
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    [DisplayName("增加")]
    public virtual async Task<bool> Add(IList<TEntity> entitys)
    {
        return await _rep.InsertRangeAsync(entitys.ToArray());
    }

    /// <summary>
    /// 更新
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    [DisplayName("更新")]
    public virtual async Task<int> Update(TEntity entity)
    {
        return await _rep.AsUpdateable(entity).IgnoreColumns(true).ExecuteCommandAsync();
    }

    /// <summary>
    /// 更新
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    [DisplayName("更新")]
    public virtual async Task<int> Update(TEntity entity, Expression<Func<TEntity, object>> updateColumns)
    {
        return await _rep.AsUpdateable(entity).IgnoreColumns(true).UpdateColumns(updateColumns).ExecuteCommandAsync();
    }

    /// <summary>
    /// 更新
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    [DisplayName("更新")]
    public virtual async Task<int> Update(Expression<Func<TEntity, bool>> wherefunc, Expression<Func<TEntity, TEntity>> updateColumns)
    {
        return await _rep.AsUpdateable().SetColumns(updateColumns).IgnoreColumns(true).Where(wherefunc).ExecuteCommandAsync();
    }

    /// <summary>
    /// 更新
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    [DisplayName("更新")]
    public virtual async Task<int> UpdateWithOptLock(TEntity entity)
    {
        return await _rep.AsUpdateable(entity).IgnoreColumns(true).ExecuteCommandWithOptLockAsync();
    }

    /// <summary>
    /// 更新
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    [DisplayName("更新")]
    public virtual async Task<int> UpdateWithOptLock(TEntity entity, Expression<Func<TEntity, object>> updateColumns)
    {
        return await _rep.AsUpdateable(entity).IgnoreColumns(true).UpdateColumns(updateColumns).ExecuteCommandWithOptLockAsync();
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [DisplayName("删除")]
    public virtual async Task<bool> Delete(long id)
    {
        return await _rep.DeleteByIdAsync(id);
    }

    public async Task<int> CountAsync(Expression<Func<TEntity, bool>> whereFunc)
    {
        return await _rep.CountAsync(whereFunc);
    }

}